package app.controllers;

import app.models.Credito;
import app.services.CreditoService;
import app.util.DateUtils;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.Validator;

@Resource
public class CreditoController {

	private Result result;
	private Validator validator;
	private CreditoService service;
	
	public CreditoController(Result result, Validator validator, CreditoService service){
		this.result = result;
		this.validator = validator;
		this.service = service;
	}
	
	
	@Get("/creditos")
	public void index() { 
		this.result.include("creditos", service.findAllOrdered());
		this.result.include("ultimosCreditos", service.findLastCreditos());
	}

	
	@Get("/credito/find")
	public void index(String filter) {
		this.result.include("filter", filter);
		this.result.include("creditos", service.findByFilter(filter));
		
		this.result.of(this).table();
	}
	
	@Get("/credito/new")
	public void form() { }
	
	
	@Post("/credito/save")
	public void save(Credito credito){
		validator.validate(credito);
		validator.onErrorForwardTo(this).form();
		
		service.create(credito);
		this.result.redirectTo(this).index();
	}
	
	public void table() { }
}
